<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess" @edit-end="handleEditEnd">
      <template #toolbar>
        <a-button type="primary" v-show="isNodeShow('sys/menu post')" @click="handleCreate">
          新增菜单
        </a-button>
        <a-button type="default" @click="expandAll"> 展开所有 </a-button>
        <a-button type="default" @click="collapseAll"> 关闭所有 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
              ifShow: isNodeShow('sys/menu put'),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
              ifShow: isNodeShow('sys/menu/:id delete'),
            },
          ]"
        />
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';

  import { columns, searchFormSchema } from './menu.data';
  import MenuApi from '/@/api/sys/menu.class';
  import { listToTree } from '/@/utils/helper/treeHelper';
  import { isNodeShow } from '/@/utils/helper/customHelper';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    components: { BasicTable, MenuDrawer, TableAction },
    setup() {
      const { createMessage } = useMessage();
      const menuApi = new MenuApi();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, expandAll, collapseAll }] = useTable({
        title: '菜单列表',
        api: () => menuApi.list(),
        afterFetch: (list) => {
          return listToTree(list, { pid: 'parentId' });
        },
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: false,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      async function handleEditEnd({ record, key, value }: Recordable) {
        createMessage.loading({ content: '数据更新中', key: '_save_data', duration: 0 });
        await menuApi.update({ id: record.id, [key]: value });
        createMessage.success({
          content: '更新完毕',
          key: '_save_data',
          duration: 2,
          onClose: () => reload(),
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await menuApi.delete(record.id);
        reload();
      }

      function handleSuccess() {
        reload();
      }

      function onFetchSuccess() {
        // 演示默认展开所有表项
        nextTick(expandAll);
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        onFetchSuccess,
        expandAll,
        collapseAll,
        isNodeShow,
        handleEditEnd,
      };
    },
  });
</script>
